//==========================================================================//
//                                                                          //
//  BMP085 Library              Version 0.1                     March 2012  //
//                                                                          //
//  Written for the Arduino Duemilanove w/ ATmega328                        //
//                                                                          //
//  BMP085/I2C Arduino Library                                              //
//                                                                          //
//  Created by JFLagarde (jeanfrancoislagarde@hotmail.com)                  //
//                                                                          //
//  This file is part of OpenWS (http://code.google.com/p/openws/)          //
//                                                                          //
//==========================================================================//
//                                                                          //
//  Notes:                                                                  //
//                                                                          //
//                                                                          //
//==========================================================================//
//                                                                          //
//  Copyright 2012 Jean-Francois Lagarde (jeanfrancoislagarde@hotmail.com)  //
//  This program is distributed under the terms of                          //
//  the GNU General Public License                                          //
//                                                                          //
//  This program is free software: you can redistribute it and/or modify    //
//  it under the terms of the GNU General Public License as published by    //
//  the Free Software Foundation, either version 3 of the License, or       //
//  (at your option) any later version.                                     //
//                                                                          //
//  This program is distributed in the hope that it will be useful,         //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of          //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           //
//  GNU General Public License for more details.                            //
//                                                                          //
//  You should have received a copy of the GNU General Public License       //
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                          //
//==========================================================================//

//http://www.engineeringtoolbox.com/air-altitude-pressure-d_462.html

#ifndef _BMP085_H_
	#define _BMP085_H_

	#if defined(ARDUINO) && ARDUINO >= 100
		#include "Arduino.h"
	#else
		#include "WProgram.h"
	#endif


	// BMP085 Modes
	#define MODE_ULTRA_LOW_POWER    0 //oversampling=0, internalsamples=1, maxconvtimepressure=4.5ms, avgcurrent=3uA, RMSnoise_hPA=0.06, RMSnoise_m=0.5
	#define MODE_STANDARD           1 //oversampling=1, internalsamples=2, maxconvtimepressure=7.5ms, avgcurrent=5uA, RMSnoise_hPA=0.05, RMSnoise_m=0.4
	#define MODE_HIGHRES            2 //oversampling=2, internalsamples=4, maxconvtimepressure=13.5ms, avgcurrent=7uA, RMSnoise_hPA=0.04, RMSnoise_m=0.3
	#define MODE_ULTRA_HIGHRES      3 //oversampling=3, internalsamples=8, maxconvtimepressure=25.5ms, avgcurrent=12uA, RMSnoise_hPA=0.03, RMSnoise_m=0.25

	class BMP085 {
		public:
			BMP085();
			void init();
			void init(byte);
			void read();
			int getTemperature();
			int getPressure();
		
		private:	
			uint8_t _dev_address;
			
			int _oss; //oversamplig for measurement
			int _pressure_waittime[4];                    // Max. Conversion Time Pressure is ms for each mode
			byte _buff[3];                      // buffer  MSB LSB XLSB

			int ac1, ac2, ac3, b1, b2, mb, mc, md;
			unsigned int ac4, ac5, ac6;
			long b5;
			
			int _lastTemperature;
			int _lastPressure;
			
			void calibrate();
			int readTemperature(void);
			int readPressure(void);
			
			void writemem(uint8_t _addr, uint8_t _val);
			void readmem(uint8_t _addr, uint8_t _nbytes, uint8_t __buff[]);
	};
#endif /*_BMP085_H_*/